package com.woniu.market_stall.web;



import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.User;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.StallService;
import com.woniu.market_stall.service.UserService;
import com.woniu.market_stall.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("user")
public class Usermanagementcontroller {

    

    @Autowired
    UserService userService;

    @Autowired
    StallService stallService;




    @GetMapping("/list")
    public ResultResponse<PageInfo<UserVo>> rlist(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                  @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                  String name){
        ResultResponse<PageInfo<UserVo>> result = null;

        try {
            PageInfo<UserVo> rolePageInfo = userService.queryByPage(now, size, name);
            result = new ResultResponse<>(200,"查询成功!",rolePageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }
    @GetMapping("/query")
    public ResultResponse<PageInfo<UserVo>> query(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                  @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                  String name){
        ResultResponse<PageInfo<UserVo>> result = null;

        try {
            PageInfo<UserVo> rolePageInfo = userService.querynameByPage(now, size, name);
            result = new ResultResponse<>(200,"查询成功!",rolePageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") long id){
        ResultResponse<Void> result = null;

        try {
            userService.removeStallByKey(id);
            result = new ResultResponse<>(200,"删除成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除失败!");
        }
        return result ;
    }



    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody UserVo userVo){
        ResultResponse<Void> result = null;

        try {
            //根据 ID 查询出来 当前数据  把前台传递过来的数据  赋值到新查询出来的对象上
            User u = userService.queryPerByKey(userVo.getId());
            u.setUsername(userVo.getUsername());
            u.setEmail(userVo.getEmail());
            u.setCellphone(userVo.getCellphone());
            u.setUpdateTime(new Date());
            u.setStatus(userVo.getStatus());
            userService.modify(u);
            result = new ResultResponse<>(200,"修改成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改失败!");
        }
        return result ;
    }


    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody UserVo userVo){
        ResultResponse<Void> result = null;

        try {
            userVo.setUpdateTime(new Date());
            userVo.setInsertTime(new Date());
            userService.add(userVo);
            result = new ResultResponse<>(200,"添加成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加失败!");
        }
        return result ;
    }

    @GetMapping("/showAllUser")
    public ResultResponse<List<UserVo>> showAllUser(){
        ResultResponse<List<UserVo>> result = null;
        try {
            List<UserVo> userVos = userService.queryAllUser();
            result = new ResultResponse<>(200,"查询所有用户成功!",userVos);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询所有用户失败!");
        }
        return result ;
    }



}
